<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    rankingType:[] as Array<any>,
    rankingRuleType:[] as Array<any>
  })
  interface formQuert {
    rank_id:number
    custom_id:number
    if_day:number
    if_all:number
    rank_type:number
    score_rule:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    rank_type: [{ required: true, message: "请选择排榜规则类型", trigger: "blur" }],
    score_rule: [{ required: true, message: "请选择榜单类型", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { rank_id } = form
      if(rank_id) {
        await gamesApi.customRankEdit(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      else {
        await gamesApi.customRankAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const {
      id:rank_id=0, custom_id=0, if_day=0, if_all=0,
      game_name='', rank_type_text='', rank_type='', score_rule='',
      rankingType=[], rankingRuleType=[]
    } = query
    if(!custom_id) return ToastWarning('缺少活动ID')
    const obj = {
      custom_id, if_day, if_all,
      rank_type, score_rule,
    } as any
    if(rank_id) obj['rank_id'] = rank_id
    Object.assign(form,obj)
    action.rankingType = rankingType
    action.rankingRuleType = rankingRuleType
    action.dialogFormVisible = true
    action.title = rank_id?`编辑榜单：${game_name}-${rank_type_text}`:'创建榜单'
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="36%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="榜单">
          <el-checkbox
            v-model="form.if_day"
            label="日榜"
            :true-value="1"
            :false-value="2"
          />
          <el-checkbox
            v-model="form.if_all"
            label="总榜"
            :true-value="1"
            :false-value="0"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="榜单类型" prop="rank_type">
          <el-select v-model="form.rank_type" placeholder="请选择榜单类型">
            <el-option
              v-for="item in action.rankingType"
              :label="item.text"
              :value="item.type"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="排榜规则" prop="score_rule">
          <el-select v-model="form.score_rule" placeholder="请选择排榜规则类型">
            <el-option
              v-for="item in action.rankingRuleType"
              :label="item.text"
              :value="item.type"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>